package cn.iocoder.yudao.module.alipay.service.cpsactivity;

import cn.iocoder.yudao.module.alipay.didi.client.DunionClientFactory;
import cn.iocoder.yudao.module.alipay.didi.client.UnionClient;
import cn.iocoder.yudao.module.alipay.didi.models.DunionClientConfig;
import cn.iocoder.yudao.module.alipay.didi.models.LinkResponse;
import cn.iocoder.yudao.module.alipay.didi.models.Result;
import cn.iocoder.yudao.module.alipay.enums.DidiEnum;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import cn.iocoder.yudao.module.alipay.controller.admin.cpsactivity.vo.*;
import cn.iocoder.yudao.module.alipay.dal.dataobject.cpsactivity.CpsActivityDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;

import cn.iocoder.yudao.module.alipay.convert.cpsactivity.CpsActivityConvert;
import cn.iocoder.yudao.module.alipay.dal.mysql.cpsactivity.CpsActivityMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.alipay.enums.ErrorCodeConstants.*;

/**
 * cps活动 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class CpsActivityServiceImpl implements CpsActivityService {

    @Resource
    private CpsActivityMapper cpsActivityMapper;

    @Override
    public Long createCpsActivity(CpsActivityCreateReqVO createReqVO) {
        // 插入
        CpsActivityDO cpsActivity = CpsActivityConvert.INSTANCE.convert(createReqVO);
        cpsActivityMapper.insert(cpsActivity);
        // 返回
        return cpsActivity.getId();
    }

    @Override
    public void updateCpsActivity(CpsActivityUpdateReqVO updateReqVO) {
        // 校验存在
        validateCpsActivityExists(updateReqVO.getId());
        // 更新
        CpsActivityDO updateObj = CpsActivityConvert.INSTANCE.convert(updateReqVO);
        cpsActivityMapper.updateById(updateObj);
    }

    @Override
    public void deleteCpsActivity(Long id) {
        // 校验存在
        validateCpsActivityExists(id);
        // 删除
        cpsActivityMapper.deleteById(id);
    }

    private void validateCpsActivityExists(Long id) {
        if (cpsActivityMapper.selectById(id) == null) {
            throw exception(CPS_ACTIVITY_NOT_EXISTS);
        }
    }

    @Override
    public CpsActivityDO getCpsActivity(Long id) {
        return cpsActivityMapper.selectById(id);
    }

    @Override
    public List<CpsActivityDO> getCpsActivityList(Collection<Long> ids) {
        return cpsActivityMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<CpsActivityDO> getCpsActivityPage(CpsActivityPageReqVO pageReqVO) {
        return cpsActivityMapper.selectPage(pageReqVO);
    }

    @Override
    public List<CpsActivityDO> getCpsActivityList(CpsActivityExportReqVO exportReqVO) {
        return cpsActivityMapper.selectList(exportReqVO);
    }

    @Override
    public Object getGenerate(CpsActivityExportReqVO pageVO) {
        CpsActivityDO cpsActivityDO = cpsActivityMapper.selectById(pageVO.getId());

        Map<String,Object> resultMap = new HashMap<>();
        //初始化配置
        DunionClientConfig config = new DunionClientConfig.Builder()
                .appKey(DidiEnum.APP_KEY.getDidiCode())
                .accessKey(DidiEnum.ASSESS_KEY.getDidiCode()).timeout(3*1000).build();

        //使用DunionClientConfig创建Client
        UnionClient unionClient = DunionClientFactory.build(config).getUnionClient();

        //调用接口
        //取链
      /*  Result<LinkResponse> res = unionClient.generateH5Link(208743698546L,
                7168787740828799249L, "1212121215555786", 0);*/
        Result<LinkResponse> miniLink = unionClient.generateMiniLink(Long.parseLong(cpsActivityDO.getActivityId()),
                Long.parseLong(cpsActivityDO.getPromotionId()), "pubTextAdmin", 0);
        Result<LinkResponse> h5Link = unionClient.generateH5Link(Long.parseLong(cpsActivityDO.getActivityId()),
                Long.parseLong(cpsActivityDO.getPromotionId()), "pubTextAdmin", 0);

/*
        if(miniLink != null && miniLink.isSuccess() && miniLink.getModel() != null){
            System.out.println("====== Result<LinkResponse>======");
            System.out.println(res.getModel().getData().getAppId());
            System.out.println(res.getModel().getData().getAppSource());
            System.out.println(res.getModel().getData().getDsi());
            System.out.println(res.getModel().getData().getLink());
            resultMap.put("miniLink",miniLink);
        }
        if(h5Link != null && h5Link.isSuccess() && h5Link.getModel() != null){
            System.out.println("====== Result<LinkResponse>======");
            System.out.println(res.getModel().getData().getAppId());
            System.out.println(res.getModel().getData().getAppSource());
            System.out.println(res.getModel().getData().getDsi());
            System.out.println(res.getModel().getData().getLink());
            resultMap.put("h5Link",h5Link);
//            Result<LinkResponse> h5Code = unionClient.generateH5Code(h5Link.getModel().getData().getDsi());
        }*/





        return resultMap;
    }

}
